package com.yanqu.road.dao.impl.activity.peakstronghold;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakstronghold.CrossPeakStrongholdUserGuessData;
import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossPeakStrongholdUserGuessDataDaoImpl extends TempDataObjectDao<CrossPeakStrongholdUserGuessData> {
    public static String getDataSql = "select * from t_c_activity_peak_stronghold_guess where activityId=? and unionGroup=?";

    public Map<Long, CrossPeakStrongholdUserGuessData> getData(int activityId, int unionGroup) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossPeakStrongholdUserGuessData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossPeakStrongholdUserGuessData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateSql = "update t_c_activity_peak_stronghold_guess set " +
            "guessWinMap=?,guessFinalsMap=? " +
            "where activityId=? and userId=? and unionGroup=?";

    @Override
    protected boolean updateData(CrossPeakStrongholdUserGuessData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGuessWinMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGuessFinalsMap())));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnionGroup()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSql = "insert into t_c_activity_peak_stronghold_guess" +
            " (activityId,unionGroup,userId,guessWinMap,guessFinalsMap) " +
            "values (?,?,?,?,?)";

    @Override
    protected boolean addData(CrossPeakStrongholdUserGuessData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getUnionGroup()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGuessWinMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGuessFinalsMap())));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossPeakStrongholdUserGuessData parseData(ResultSet rs) throws SQLException {
        CrossPeakStrongholdUserGuessData data = new CrossPeakStrongholdUserGuessData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUnionGroup(rs.getInt("unionGroup"));
        data.setUserId(rs.getLong("userId"));
        data.setGuessWinMap(JSON.parseObject(rs.getString("guessWinMap"), new TypeReference<ConcurrentHashMap<Integer, List<String>>>() {
        }));
        data.setGuessFinalsMap(JSON.parseObject(rs.getString("guessFinalsMap"), new TypeReference<List<String>>() {
        }));
        return data;
    }
}
